package com.dopies.bsdmall.activity;


import java.net.URLEncoder;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.android.app.sdk.AliPay;
import com.dopies.BeeFramework.activity.BaseActivity;
import com.dopies.bsdmall.R;
import com.dopies.bsdmall.alipay.Keys;
import com.dopies.bsdmall.alipay.Rsa;

public class PayWebActivity extends BaseActivity {
	
	public static final String TAG = "alipay-sdk";

	private static final int RQF_PAY = 1;

	private static final int RQF_LOGIN = 2;
	
	private TextView title;
	private ImageView back;
	private WebView webView;
	
	private String data;
	private String order_no;
	private String price;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_web);
//		
//		Intent intent = getIntent();
//		data = intent.getStringExtra("html");
//		
//		title = (TextView) findViewById(R.id.top_view_text);
//        Resources resource = (Resources) getBaseContext().getResources();
//        String pay=resource.getString(R.string.pay);
//  		title.setText(pay);
//		
//		back = (ImageView) findViewById(R.id.top_view_back);
//		back.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				finish();
//			}
//		});
//		
//		webView = (WebView) findViewById(R.id.pay_web);
//		
//		webView.loadDataWithBaseURL(null,data,"text/html","utf-8",null);
//		
//		webView.setWebViewClient(new WebViewClient() { // 通过webView打开链接，不调用系统浏览器
//
//			@Override
//			public boolean shouldOverrideUrlLoading(WebView view, String url) {
//				// TODO Auto-generated method stub
//				view.loadUrl(url);
//				return true;
//			}
//		});
//		
//		WebSettings webSettings = webView.getSettings();  
//		webSettings.setJavaScriptEnabled(true); 
//		webSettings.setBuiltInZoomControls(true);
		
		try {
			Intent intent = getIntent();
//			data = intent.getStringExtra("html");
			order_no = intent.getStringExtra("order_no");
			price = intent.getStringExtra("price");
			String info = getNewOrderInfo();
			String sign = Rsa.sign(info, Keys.PRIVATE);
			sign = URLEncoder.encode(sign);
			info += "&sign=\"" + sign + "\"&" + getSignType();
			Log.i(TAG, "info = " + info);

			final String orderInfo = info;
			new Thread() {
				@Override
				public void run() {
					AliPay alipay = new AliPay(PayWebActivity.this, mHandler);
					
					//设置为沙箱模式，不设置默认为线上环境
//					alipay.setSandBox(true);

					String result = alipay.pay(orderInfo);

					Log.i(TAG, "result = " + result);
					Message msg = new Message();
					msg.what = RQF_PAY;
					msg.obj = result;
					mHandler.sendMessage(msg);
				}
			}.start();

		} catch (Exception ex) {
			ex.printStackTrace();
			Toast.makeText(PayWebActivity.this, R.string.remote_call_failed,
					Toast.LENGTH_SHORT).show();
		}
		
	}
	
	
	private String getSignType() {
		return "sign_type=\"RSA\"";
	}
	
	private String getNewOrderInfo() {
		StringBuilder sb = new StringBuilder();
		sb.append("partner=\"");
		sb.append(Keys.DEFAULT_PARTNER);
		sb.append("\"&out_trade_no=\"");
		sb.append(order_no);
		sb.append("\"&subject=\"");
		sb.append(order_no);
		sb.append("\"&body=\"");
		sb.append("安卓客户端购买");
		sb.append("\"&total_fee=\"");
		sb.append(price);
		sb.append("\"&notify_url=\"");
		// 网址需要做URL编码
		sb.append(URLEncoder.encode("http://218.244.145.64/includes/modules/payment/alipay.php"));
		sb.append("\"&service=\"mobile.securitypay.pay");
		sb.append("\"&_input_charset=\"UTF-8");
		sb.append("\"&return_url=\"");
		sb.append(URLEncoder.encode("http://218.244.145.64/includes/modules/payment/alipay.php"));
		sb.append("\"&payment_type=\"1");
		sb.append("\"&seller_id=\"");
		sb.append(Keys.DEFAULT_SELLER);
		// 如果show_url值为空，可不传
		// sb.append("\"&show_url=\"");
		sb.append("\"&it_b_pay=\"1m");
		sb.append("\"");

		return new String(sb);
	}

}
